﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using InnovTech.Util.Crypto;
using System.IO;
using InnovTech.Data.Dropbox;
using AppLimit.CloudComputing.SharpBox;

namespace Dropbox.DataHouse.CommandLine
{
    class Program
    {
        static readonly string RepaireFileList = "-repairlist";



        static readonly string fileInfosListName = "list";
        static readonly string fileInfosListDir = "/DataWare";
        static readonly string dataDir = "/DataWare/Data";

        static void Main(string[] args)
        {
            if (args[0].Contains(RepaireFileList))
                RepairFileList();
        }

        static void RepairFileList()
        {
            DropboxClient client = CreateStorage();

            client.Open();
            ICloudDirectoryEntry dirEntry = client.GetFolder(dataDir);
            var q = from file in dirEntry
                    select file;

            ObservableCollection<FileInformation> fileInfos = new ObservableCollection<FileInformation>();

            q.ToList().ForEach(file =>
            {
                FileInformation fi = new FileInformation()
                {
                    Name = file.Name,
                     Dir = dataDir,
                      LastModification = file.Modified,
                       Length = file.Length,
                };
                fileInfos.Add(fi);
            });

            client.Open();
            client.Serialize(fileInfos, fileInfosListName, fileInfosListDir);

            client.Close();
        }

        static DropboxClient CreateStorage()
        {

            StreamCryptor cryptor = new RijndaelCryptor();

            DropboxLoginInfo login = null;
            using (FileStream fs = File.OpenRead(Directory.GetCurrentDirectory() + "\\accessToken"))
            {
                login = cryptor.Decrypt(fs) as DropboxLoginInfo;
                fs.Close();

            }

            return new DropboxClient(login, cryptor);
        }

       
    }
}
